﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Noris.Tools.TraceVisualiser.Support;
using Noris.Tools.TraceVisualiser.Interface;

namespace Noris.Tools.TraceVisualiser.Forms
{
    internal partial class EventDetailForm : Noris.Tools.TraceVisualiser.Components.DForm
    {
        #region Konstrukce
        public EventDetailForm()
        {
            InitializeComponent();
            this._Init();
        }
        private void _Init()
        {
            this._ViewMode = EventDetailViewMode.Detail;

            this.DetailLayout = (EventDetailLayoutSet)Steward.GetFromCache(typeof(EventDetailLayoutSet));
            this.ToolDetail.Image = TraceVisualiser.Pics.PicLibrary22.ViewSplitDetail_FromFile;
            this.ToolDetail.Size = new Size(26, 26);
            this.ToolDetail.Click += new EventHandler(ToolDetail_Click);

            this.ToolGrid.Image = TraceVisualiser.Pics.PicLibrary22.ViewGrid_FromFile;
            this.ToolGrid.Size = new Size(26, 26); 
            this.ToolGrid.Click += new EventHandler(ToolGrid_Click);

            this.ToolGrid.ImageScaling = ToolStripItemImageScaling.None;
            this.ToolGrid.AutoSize = true;
            this.ToolGrid.Width = 26;

            this.EventDetailGrid.ScrollBars = ScrollBars.Both;

            this.EventList.SelectedIndexChanged += new EventHandler(EventList_SelectedIndexChanged);

			this.EventDetailGrid.RowHeightChanged += new DataGridViewRowEventHandler(EventDetailGrid_RowHeightChanged);
			this.EventDetailGrid.ColumnWidthChanged += new DataGridViewColumnEventHandler(EventDetailGrid_ColumnWidthChanged);
            this.EventDetailGrid.CellBeginEdit += new DataGridViewCellCancelEventHandler(EventDetailGrid_CellBeginEdit);
            this.EventDetailGrid.CellEndEdit += new DataGridViewCellEventHandler(EventDetailGrid_CellEndEdit);
			this.EventDetailGrid.TopLeftHeaderCell.ToolTipText = "Right click:" + Environment.NewLine + "Show setting of Column.Visible";

            this.CancelButton = this.CloseBtn;
        }
        private void CloseBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion
        #region Přepínání režimu zobrazení
        /// <summary>
        /// Režim zobrazení, výchozí je Detail
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public EventDetailViewMode ViewMode
        {
            get { return this._ViewMode; }
            set
            {
                this._ViewMode = value;
                this._ShowItems();
            }
        }
        /// <summary>
        /// Text, který se zobrazí v záhlaví prvního sloupce Gridu, který zobrazuje titulek události
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string TitleColumnText { get; set; }
        void ToolDetail_Click(object sender, EventArgs e)
        {
            this._ViewMode = EventDetailViewMode.Detail;
            this._ShowItems();
        }
        void ToolGrid_Click(object sender, EventArgs e)
        {
            this._ViewMode = EventDetailViewMode.Grid;
            this._ShowItems();
        }
        private EventDetailViewMode _ViewMode;
        /// <summary>
        /// Zajistí zobrazení seznamu položek.
        /// </summary>
        private void _ShowItems()
        {
            EventDetailViewMode viewMode = this._ViewMode;
            this.ToolDetail.Checked = (viewMode == EventDetailViewMode.Detail);
            this.ToolGrid.Checked = (viewMode == EventDetailViewMode.Grid);
            this.EventList.Items.Clear();
            switch (viewMode)
            {
                case EventDetailViewMode.Detail:
                    this._ShowItemsDetail();
                    break;
                case EventDetailViewMode.Grid:
                    this._ShowItemsGrid();
                    break;
            }
        }
		#endregion
        #region Zpracování seznamu vstupních položek typu IShowDetail - v režimu Detail
        private void _ShowItemsDetail()
        {
			// Preselect item:
			int itemIndex = 0;
			string detailName = null;
			if (this.EventDetailGrid.Rows.Count > 0 && this.EventDetailGrid.CurrentCell != null)
			{
				itemIndex = this.EventDetailGrid.CurrentCell.RowIndex;             // Číslo položky
				detailName = this.EventDetailGrid.CurrentCell.OwningColumn.Name;   // Název sloupce
			}

            // Columns:
            this.EventDetailGrid.AllowUserToOrderColumns = false;
            this.EventDetailGrid.Columns.Clear();
            this._GridCellSizeChangedEventsSupress = true;
            this.EventDetailGrid.Columns.Add(this._CreateGridColumn(new MasterDetailItem("Name", null, new Size(100, 21)), "Name", System.Windows.Forms.DataGridViewAutoSizeColumnMode.None, true));
            this.EventDetailGrid.Columns.Add(this._CreateGridColumn(new MasterDetailItem("Value", null, new Size(500, 21)), "Value", System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill, false));
            this._GridCellSizeChangedEventsSupress = false;

			int selectIndex = -1;
            int cnt = 0;
            if (this._ShowDetailItems != null)
            {
                foreach (IMasterDetails detail in this._ShowDetailItems)
                {
                    if (detail != null && !String.IsNullOrEmpty(detail.MasterName))  // Nebudu vyhodnocovat podmínku: ( && detail.Details != null), protože to vede k zbytečnému generování obsahu v Detail.Get !!!
                    {
						if (cnt == itemIndex)
							selectIndex = cnt;
                        this.EventList.Items.Add(new MasterDetail(detail));
                        cnt++;
                    }
                }
            }
            this.StatusLabel.Text = "Počet vybraných událostí: " + cnt.ToString();
            this.SplitCnt.Panel1Collapsed = (cnt <= 1);
			if (selectIndex >= 0)
				this.EventList.SelectedIndex = selectIndex;             // Převezme si to event a zobrazí data z první položky

            if (cnt > 1)
                this.EventList.Focus();
            else
                this.EventDetailGrid.Focus();
        }
        /// <summary>
        /// Naplní do Gridu data jako Detail
        /// </summary>
        /// <param name="masterDetail"></param>
        void _GridDetailFill(MasterDetail masterDetail)
        {
            if (this.EventDetailGrid.Rows.Count > 0)
                this.EventDetailGrid.Rows.Clear();
			
			this.GridContextMenu = new ContextMenuStrip();
			this.GridContextMenu.RenderMode = ToolStripRenderMode.Professional;
			this.GridContextMenu.DropShadowEnabled = true;
			this.EventDetailGrid.TopLeftHeaderCell.ContextMenuStrip = this.GridContextMenu;
            this.EventDetailGrid.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText;

			foreach (MasterDetailItem rowDetail in masterDetail.Details)
            {
				if (rowDetail.ShowInDetail)
				{
					DataGridViewRow row = new DataGridViewRow();
					row.Cells.Add(new DataGridViewTextBoxCell() { Value = rowDetail.Name });
					DataGridViewTextBoxCell valueCell = new DataGridViewTextBoxCell() { Value = rowDetail.Value };
					valueCell.Style.WrapMode = DataGridViewTriState.True;
					row.Cells.Add(valueCell);
					// Aplikovat layout:
					EventDetailLayoutItem layoutItem = this.LayoutItemGet(rowDetail);
                    row.Tag = rowDetail;
                    row.Visible = layoutItem.ItemVisibleDetail;
                    if (layoutItem.CellSize.Height > 9)
    					row.Height = layoutItem.CellSize.Height;
					row.HeaderCell.ContextMenuStrip = this.GridContextMenu;
					this.GridContextMenu.Items.Add(layoutItem.CreateContextItemDetail());
					this.EventDetailGrid.Rows.Add(row);
				}
            }

            if (this.EventDetailGrid.Rows.Count > 0)
                this.EventDetailGrid.CurrentCell = this.EventDetailGrid.Rows[0].Cells[0];
        }
        /// <summary>
        /// Událost po změně vybrané položky = zobrazit dostupné detaily
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void EventList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this._ViewMode != EventDetailViewMode.Detail) return;

            this.EventDetailGrid.Rows.Clear();

            object selectedObject = this.EventList.SelectedItem;
            if (selectedObject == null ) return;
            MasterDetail masterDetail = selectedObject as MasterDetail;
            if (masterDetail == null) return;

            this._GridDetailFill(masterDetail);
        }
		#endregion
        #region Zpracování seznamu vstupních položek typu IShowDetail - v režimu Grid
        private void _ShowItemsGrid()
        {
            this.SplitCnt.Panel1Collapsed = true;

            this.EventDetailGrid.Visible = false;
            this.EventDetailGrid.Hide();
            this.EventDetailGrid.SuspendLayout();

            this.EventDetailGrid.AllowUserToOrderColumns = true;
            this.EventDetailGrid.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;

            // Grid columns:
            Dictionary<string, int> columns = new Dictionary<string, int>();
            this.EventDetailGrid.Columns.Clear();
            string titleColumnText = this.TitleColumnText;
            if (String.IsNullOrEmpty(titleColumnText))
                titleColumnText = "Událost";
            MasterDetailItem columnFirst = new MasterDetailItem("EventNameCol", null, new Size(100, 21));
            this._AddColumnToGrid(columnFirst, titleColumnText, System.Windows.Forms.DataGridViewAutoSizeColumnMode.None, columns, true);

            int cnt = 0;
            List<DataGridViewRow> rows = new List<DataGridViewRow>();
            if (this._ShowDetailItems != null)
            {
                foreach (IMasterDetails detail in this._ShowDetailItems)
                {   // Každý vstupní objekt = 1 řádek:
                    DataGridViewRow row = new DataGridViewRow();
                    this._AddValueToRow(row, columns, columnFirst, detail.MasterName);
					foreach (MasterDetailItem rowDetail in detail.Details)
					{
						if (rowDetail.ShowInGrid)
							this._AddValueToRow(row, columns, rowDetail, rowDetail.Value);
					}
                    rows.Add(row);
                    cnt++;
                }
            }

            this.EventDetailGrid.Rows.AddRange(rows.ToArray());
            this.StatusLabel.Text = "Počet vybraných událostí: " + cnt.ToString();

            this.EventDetailGrid.Visible = true;
            this.EventDetailGrid.Show();
            this.EventDetailGrid.ResumeLayout(true);
            this.EventDetailGrid.Focus();
        }
		#endregion
		#region Obsluha gridu (columns / rows / cells)
        private void _AddValueToRow(DataGridViewRow row, Dictionary<string, int> columns, MasterDetailItem rowDetail, object cellValue)
        {
            int columnIdx;
            string columnName = rowDetail.Name;
            if (!columns.TryGetValue(columnName, out columnIdx))
                columnIdx = _AddColumnToGrid(rowDetail, rowDetail.Name /* nebo zde může být i nějaký Text ? */, DataGridViewAutoSizeColumnMode.None, columns, false);

            // Naplním do řádku buňky tak, aby řádek obsahoval potřebný počet Cells:
            while (row.Cells.Count <= columnIdx)
                row.Cells.Add(new DataGridViewTextBoxCell());

            DataGridViewTextBoxCell valueCell = new DataGridViewTextBoxCell();
            valueCell.Value = cellValue;
            valueCell.Style.WrapMode = DataGridViewTriState.True;
            valueCell.ReadOnly = false;
            row.Cells[columnIdx] = valueCell;
        }
        private int _AddColumnToGrid(MasterDetailItem rowDetail, string headerText, DataGridViewAutoSizeColumnMode autoSizeMode, Dictionary<string, int> columns, bool readOnly)
        {
            int columnIdx = this.EventDetailGrid.Columns.Count;
            this.EventDetailGrid.Columns.Add(this._CreateGridColumn(rowDetail, headerText, autoSizeMode, readOnly));
            columns.Add(rowDetail.Name, columnIdx);
            return columnIdx;
        }
        private DataGridViewTextBoxColumn _CreateGridColumn(MasterDetailItem rowDetail, string headerText, DataGridViewAutoSizeColumnMode autoSizeMode, bool readOnly)
        {
            DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
            column.Name = rowDetail.Name;
            column.HeaderText = headerText;
            column.AutoSizeMode = autoSizeMode;
            column.ReadOnly = readOnly;
            
            // this.EventDetailGrid.
			// Layout:
			column.Tag = rowDetail;
            int width = this.ItemWidthGet(rowDetail);
            if (width < 0 || autoSizeMode == DataGridViewAutoSizeColumnMode.Fill)
                column.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            else
            {
                column.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
                if (width > 0)
                    column.Width = width;
            }
			column.Visible = this.ItemIsVisibleInGrid(rowDetail);

            return column;
        }
        /// <summary>
        /// Událost, kdy uživatel zahajuje editaci obsahu buňky
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EventDetailGrid_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            this._EditedCellValue = this.EventDetailGrid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
        }
        /// <summary>
        /// Událost, kdy uživatel dokončuje editaci obsahu buňky
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EventDetailGrid_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            this.EventDetailGrid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = this._EditedCellValue;
            this.EventDetailGrid.CancelEdit();
        }
        private object _EditedCellValue;
        #endregion
		#region Layout
        /// <summary>
        /// Potlačí (true) výkon v eventech EventDetailGrid_ColumnWidthChanged a EventDetailGrid_RowHeightChanged.
        /// Platí po dobu přidávání sloupců / řádků.
        /// </summary>
        private bool _GridCellSizeChangedEventsSupress;
		/// <summary>
		/// Uživatel změnil šířku sloupce v grafu
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		void EventDetailGrid_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
		{
            if (!this._GridCellSizeChangedEventsSupress)
            {
                if (this._ViewMode == EventDetailViewMode.Grid)
                {
                    if (e.Column.AutoSizeMode == DataGridViewAutoSizeColumnMode.None)
                    {   // Pouze pro sloupce s ručním řízením šířky:
                        MasterDetailItem rowDetail = e.Column.Tag as MasterDetailItem;
                        if (rowDetail != null)
                            this.ItemWidthSet(rowDetail, e.Column.Width);
                    }
                }
            }
		}
		/// <summary>
		/// Uživatel změnil výšku sloupce v grafu
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		void EventDetailGrid_RowHeightChanged(object sender, DataGridViewRowEventArgs e)
		{
            if (!this._GridCellSizeChangedEventsSupress)
            {
                if (this._ViewMode == EventDetailViewMode.Detail)
                {
                    MasterDetailItem rowDetail = e.Row.Tag as MasterDetailItem;
                    if (rowDetail != null)
                        this.ItemHeightSet(rowDetail, e.Row.Height);
                }
            }
		}
		/// <summary>
		/// Najde a vrátí položku Layoutu pro dané jméno
		/// </summary>
		/// <param name="name"></param>
		/// <returns></returns>
        internal EventDetailLayoutItem LayoutItemGet(MasterDetailItem rowDetail)
		{
			return DetailLayout.LayoutItemGet(rowDetail);
		}
		/// <summary>
		/// Vrátí true, pokud daná položka detailu má být Visible v pohledu Detail
		/// </summary>
		/// <param name="name"></param>
		/// <returns></returns>
        internal bool ItemIsVisibleInDetail(MasterDetailItem rowDetail)
		{
			return DetailLayout.ItemIsVisibleInDetail(rowDetail);
		}
		/// <summary>
		/// Vrátí true, pokud daná položka detailu má být Visible v pohledu Grid
		/// </summary>
		/// <param name="name"></param>
		/// <returns></returns>
        internal bool ItemIsVisibleInGrid(MasterDetailItem rowDetail)
		{
			return DetailLayout.ItemIsVisibleInGrid(rowDetail);
		}
		/// <summary>
		/// Vrátí šířku sloupce s daným detailem
		/// </summary>
		/// <param name="name"></param>
		/// <returns></returns>
		internal int ItemWidthGet(MasterDetailItem rowDetail)
		{
			return DetailLayout.ItemWidthGet(rowDetail);
		}
		/// <summary>
		/// Uloží šířku sloupce
		/// </summary>
		/// <param name="name"></param>
		/// <param name="width"></param>
		internal void ItemWidthSet(MasterDetailItem rowDetail, int width)
		{
			DetailLayout.ItemWidthSet(rowDetail, width);
		}
		/// <summary>
		/// Vrátí výšku řádku s daným detailem
		/// </summary>
		/// <param name="name"></param>
		/// <returns></returns>
		internal int ItemHeightGet(MasterDetailItem rowDetail)
		{
			return DetailLayout.ItemHeightGet(rowDetail);
		}
		/// <summary>
		/// Uloží výšku řádku
		/// </summary>
		/// <param name="name"></param>
		/// <param name="width"></param>
		internal void ItemHeightSet(MasterDetailItem rowDetail, int height)
		{
			DetailLayout.ItemHeightSet(rowDetail, height);
		}
		internal EventDetailLayoutSet DetailLayout { get; private set; }
		internal ContextMenuStrip GridContextMenu { get; private set; }
		#endregion
        #region Objekty k zobrazení
        /// <summary>
        /// Seznam objektů k zobrazení.
        /// Vložení hodnoty = provede zobrazení.
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public IEnumerable<IMasterDetails> ShowDetailItems
        {
            get { return this._ShowDetailItems; }
            set { this._ShowDetailItems = value; _ShowItems(); }
        }
        private IEnumerable<IMasterDetails> _ShowDetailItems;
        protected class MasterDetail
        {
            public MasterDetail(IMasterDetails detail)
            {
                this.Content = detail;
            }
            public override string ToString()
            {
                return this.Content.MasterName;
            }
            /// <summary>
            /// Uložený objekt IMasterDetails
            /// </summary>
            public IMasterDetails Content { get; private set; }
            /// <summary>
            /// Soupis detailů, kde Key = název detailu a Value = hodnota
            /// </summary>
			public IEnumerable<MasterDetailItem> Details { get { return this.Content.Details; } }
        }
        #endregion
    }
	#region classes EventDetailLayoutSet, EventDetailLayoutItem : paměť Layoutu v okně EventDetailForm
	/// <summary>
	/// Paměť Layoutu pro zobrazení detailních informací
	/// </summary>
	internal class EventDetailLayoutSet : IInitialize, IDisposable
	{
        #region Konstrukce, property
		public EventDetailLayoutSet()
		{
			this.LayoutItems = new Dictionary<string, EventDetailLayoutItem>();
		}
		/// <summary>
		/// Najde a vrátí položku Layoutu pro dané jméno
		/// </summary>
		/// <param name="name"></param>
		/// <returns></returns>
        internal EventDetailLayoutItem LayoutItemGet(MasterDetailItem rowDetail)
		{
            if (String.IsNullOrEmpty(rowDetail.Name)) return EventDetailLayoutItem.DefaultDetail;
			EventDetailLayoutItem item = this._FindOrCreateItem(rowDetail);
			return item;
		}
		/// <summary>
		/// Vrátí true, pokud daná položka detailu má být Visible v pohledu Detail
		/// </summary>
		/// <param name="name"></param>
		/// <returns></returns>
        internal bool ItemIsVisibleInDetail(MasterDetailItem rowDetail)
		{
            if (String.IsNullOrEmpty(rowDetail.Name)) return true;
			EventDetailLayoutItem item = this._FindOrCreateItem(rowDetail);
			return item.ItemVisibleGrid;
		}
		/// <summary>
		/// Vrátí true, pokud daná položka detailu má být Visible v pohledu Grid
		/// </summary>
		/// <param name="name"></param>
		/// <returns></returns>
        internal bool ItemIsVisibleInGrid(MasterDetailItem rowDetail)
		{
			if (String.IsNullOrEmpty(rowDetail.Name)) return true;
			EventDetailLayoutItem item = this._FindOrCreateItem(rowDetail);
			return item.ItemVisibleGrid;
		}
		/// <summary>
		/// Vrátí šířku sloupce s daným detailem
		/// </summary>
		/// <param name="name"></param>
		/// <returns></returns>
        internal int ItemWidthGet(MasterDetailItem rowDetail)
		{
            if (String.IsNullOrEmpty(rowDetail.Name)) return EventDetailLayoutItem.DefaultSize.Width;
            EventDetailLayoutItem item = this._FindOrCreateItem(rowDetail);
			return item.CellSize.Width;
		}
		/// <summary>
		/// Uloží šířku sloupce
		/// </summary>
		/// <param name="name"></param>
		/// <param name="width"></param>
        internal void ItemWidthSet(MasterDetailItem rowDetail, int width)
		{
            if (String.IsNullOrEmpty(rowDetail.Name)) return;
            EventDetailLayoutItem item = this._FindOrCreateItem(rowDetail);
			item.CellSize = new Size(width, item.CellSize.Height);
		}
		/// <summary>
		/// Vrátí výšku řádku s daným detailem
		/// </summary>
		/// <param name="name"></param>
		/// <returns></returns>
        internal int ItemHeightGet(MasterDetailItem rowDetail)
		{
            if (String.IsNullOrEmpty(rowDetail.Name)) return EventDetailLayoutItem.DefaultSize.Height;
            EventDetailLayoutItem item = this._FindOrCreateItem(rowDetail);
			return item.CellSize.Height;
		}
		/// <summary>
		/// Uloží výšku řádku
		/// </summary>
		/// <param name="name"></param>
		/// <param name="width"></param>
		internal void ItemHeightSet(MasterDetailItem rowDetail, int height)
		{
			if (String.IsNullOrEmpty(rowDetail.Name)) return;
			EventDetailLayoutItem item = this._FindOrCreateItem(rowDetail);
			item.CellSize = new Size(item.CellSize.Width, height);
		}
		/// <summary>
		/// Vrátí (najde nebo vytvoří) novou položku Layoutu
		/// </summary>
		/// <param name="name"></param>
		/// <returns></returns>
        private EventDetailLayoutItem _FindOrCreateItem(MasterDetailItem rowDetail)
		{
			if (String.IsNullOrEmpty(rowDetail.Name)) return null;
			EventDetailLayoutItem item;
			if (!this.LayoutItems.TryGetValue(rowDetail.Name, out item))
			{
				item = new EventDetailLayoutItem(rowDetail.Name, rowDetail.OptimalSize);
				this.LayoutItems.Add(rowDetail.Name, item);
			}
			return item;
		}
		internal Dictionary<string, EventDetailLayoutItem> LayoutItems { get; set; }
        #endregion
        #region IInitialize, IDisposable Members
        /// <summary>
        /// Inicializace: Zde je možno načíst obsah z disku.
        /// </summary>
        public void Initialize()
        {
        }
        /// <summary>
        /// Dispose: zde je možno uložit obsah na disk.
        /// </summary>
        public void Dispose()
        {
        }
        #endregion
    }
	/// <summary>
	/// Layout jedné položky
	/// </summary>
	internal class EventDetailLayoutItem
    {
        #region Konstrukce, property
        internal EventDetailLayoutItem(string name, Size optimalSize)
		{
			this.Name = name;
			this.ItemVisibleDetail = true;
			this.ItemVisibleGrid = true;
			this.CellSize = DefaultSize;

            // Některé prvky vytvořím lepší než jiné:
            if (!optimalSize.IsEmpty)
            {
                this.CellSize = optimalSize;
            }
            else
            {
                int h = this.CellSize.Height;
                switch (name.ToLower())
                {
                    case "sqlcommand":
                        this.CellSize = new Size(250, 4 * h);
                        break;
                    case "stack":
                        this.CellSize = new Size(200, 3 * h);
                        break;
                }
            }
		}
		public string Name { get; private set; }
		public bool ItemVisibleDetail { get; set; }
		public bool ItemVisibleGrid { get; set; }
		public Size CellSize { get; set; }
		public static Size DefaultSize { get { return new Size(120, 19); } }
		/// <summary>
		/// Vygeneruje a vrátí položku kontextového menu
		/// </summary>
		/// <returns></returns>
		internal ToolStripItem CreateContextItemDetail()
		{
			ToolStripMenuItem menuItem = new ToolStripMenuItem(this.Name);
			menuItem.Name = this.Name;
			menuItem.Checked = this.ItemVisibleDetail;
			menuItem.CheckOnClick = true;
			menuItem.CheckedChanged += new EventHandler(_MenuItem_CheckedChanged);
			return menuItem;
		}
		/// <summary>
		/// Vygeneruje a vrátí položku kontextového menu
		/// </summary>
		/// <returns></returns>
		internal ToolStripItem CreateContextItemGrid()
		{
			ToolStripMenuItem menuItem = new ToolStripMenuItem(this.Name);
			menuItem.Name = this.Name;
			menuItem.Checked = this.ItemVisibleGrid;
			menuItem.CheckOnClick = true;
			menuItem.CheckedChanged += new EventHandler(_MenuItem_CheckedChanged);
			return menuItem;
		}

		void _MenuItem_CheckedChanged(object sender, EventArgs e)
		{
			if (sender == null || !(sender is ToolStripMenuItem)) return;
			ToolStripMenuItem menuItem = sender as ToolStripMenuItem;
			if (menuItem == null) return;
			string name = menuItem.Name;
			if (String.IsNullOrEmpty(name)) return;
			this.ItemVisibleDetail = menuItem.Checked;
        }
        #endregion
        #region Default item
        /// <summary>
        /// Implicitní layout, lepší než null
        /// </summary>
        internal static EventDetailLayoutItem DefaultDetail
        {
            get
            {
                EventDetailLayoutItem item = new EventDetailLayoutItem("", EventDetailLayoutItem.DefaultSize);
                return item;
            }
        }
        #endregion
    }
	#endregion
}
